# -*- coding: utf8 -*-

import os
import json
import datetime

# logger
LOG_DIR = './log'
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format':
            '[%(asctime)s: %(levelname)s] %(module)s.%(funcName)s[%(request_id)s] %(pathname)s:%(lineno)s '
            '%(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(filename)s:%(lineno)s %(message)s'
        },
    },
    'handlers': {
        'file': {
            'level':
            'DEBUG',
            'class':
            'logging.FileHandler',
            'filename':
            '{}/{}.log'.format(LOG_DIR,
                               datetime.datetime.today().strftime("%Y-%m-%d")),
            'formatter':
            'verbose'
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        }
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'bit_torrent': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': True,
        }
    },
}

# Torrent 文件后缀
TORRENT_SUFFIX = '.torrent'

# bit torrent 设置  参考 https://www.libtorrent.org/reference-Settings.html#overview
BIT_TORRENT_SETTINGS = {
    'tracker_urls': json.loads(os.environ.get('BIT_TORRENT_TRACKER_URLS', json.dumps(['http://47.93.31.219:9696/announce']))),
    'listen_interfaces': os.environ.get('BIT_TORRENT_LISTEN_INTERFACES', '0.0.0.0:7888'),
    'upload_rate_limit': int(os.environ.get('BIT_TORRENT_UPLOAD_RATE_LIMIT', -1)),
    'download_rate_limit': int(os.environ.get('BIT_TORRENT_DOWNLOAD_RATE_LIMIT', -1)),
    'connections_limit': int(os.environ.get('BIT_TORRENT_CONNECTIONS_LIMIT', 1000)),
    'tracker_backoff': int(os.environ.get('BIT_TORRENT_TRACKER_BACKOFF', 0)),
    # 'connections_limit': int(os.environ.get('BIT_TORRENT_CONNECTIONS_LIMIT', 1000)),
    # 'connections_limit': int(os.environ.get('BIT_TORRENT_CONNECTIONS_LIMIT', 1000)),
    # 'connections_limit': int(os.environ.get('BIT_TORRENT_CONNECTIONS_LIMIT', 1000)),
    # 'connections_limit': int(os.environ.get('BIT_TORRENT_CONNECTIONS_LIMIT', 1000)),
    # 'connections_limit': int(os.environ.get('BIT_TORRENT_CONNECTIONS_LIMIT', 1000)),
    # 'connections_limit': int(os.environ.get('BIT_TORRENT_CONNECTIONS_LIMIT', 1000)),
    # 'connections_limit': int(os.environ.get('BIT_TORRENT_CONNECTIONS_LIMIT', 1000)),
    'proxy_host': os.environ.get('BIT_TORRENT_PROXY_HOST', ''),
}

# ====== 守护进程(daemon) ======
# pid 文件
DAEMON_BT_CLIENT_DAEMON_PID_FILE = '/tmp/bt_client.pid'
# 状态监测时间间隔(秒)
DAEMON_CHECK_INTERVAL = 5

# 文件分类存储位置
FOLDER_CATEGORY_PATH_DICT = json.loads(os.environ.get('FOLDER_CATEGORY_PATH_DICT', json.dumps({
    'videos': './temp_videos',
    'pictures': './temp_pictures',
})))

# Torrent 文件夹根目录
TORRENT_FOLDER_ROOT = './temp_torrent_files'

# torrent file 有效期
TORRENT_FILE_VALID_TIME = 7 * 24 * 3600

# ====== websocket ======
# WEBSOCKET_SERVER_URL = os.environ.get('WEBSOCKET_SERVER_URL', 'https://websocket.bjywkd.com:9111')
WEBSOCKET_CLIENT_URL = os.environ.get('WEBSOCKET_CLIENT_URL', 'ws://websocket.bjywkd.com:9111/connection/websocket')
WEBSOCKET_SECRET = os.environ.get('WEBSOCKET_SECRET', 'f3ac4b2d-3e8e-4bc3-9a13-977cf2b43548')
# WEBSOCKET_SERVER_TIME_OUT = 3
WEBSOCKET_CHANNLE = os.environ.get('WEBSOCKET_CHANNLE', 'download_media')